package com.rskj.cashbook.service.iml;

import com.rskj.cashbook.dao.DictTypeDao;
import com.rskj.cashbook.service.IDictTypeService;
import com.rskj.cashbook.util.SnowflakeIdWorker;
import com.rskj.cashbook.vo.DictType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 字典类型服务实现类
 * @author fudaopin
 */
@Service
public class DictTypeService implements IDictTypeService {

    @Autowired
    private DictTypeDao dictTypeDao;

    @Override
    public DictType getById(String id) {
        return dictTypeDao.selectById(id);
    }

    @Override
    public DictType getByCode(String code) {
        return dictTypeDao.selectByCode(code);
    }

    @Override
    public List<DictType> getAll() {
        return dictTypeDao.selectAll();
    }

    @Override
    public List<DictType> getAllEnabled() {
        return dictTypeDao.selectAllEnabled();
    }

    @Override
    public int save(DictType dictType) {
        // 设置ID和创建时间
        dictType.setId(SnowflakeIdWorker.getId());
        dictType.setCreateDate(new Date());
        dictType.setUpdateDate(new Date());
        
        // 设置默认值
        if (dictType.getEnabled() == null) {
            dictType.setEnabled(1);
        }
        if (dictType.getSort() == null) {
            dictType.setSort(0);
        }
        
        return dictTypeDao.insert(dictType);
    }

    @Override
    public int update(DictType dictType) {
        // 设置更新时间
        dictType.setUpdateDate(new Date());
        return dictTypeDao.update(dictType);
    }

    @Override
    public int delete(String id) {
        return dictTypeDao.deleteById(id);
    }

    @Override
    public int deleteByCode(String code) {
        return dictTypeDao.deleteByCode(code);
    }

    @Override
    public boolean isCodeExists(String code) {
        return dictTypeDao.countByCode(code) > 0;
    }
}
